package cn.yunyichina.scheduler.jobs.service.drug.impl;

import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.dubbo.service.DrugService;
import cn.yunyichina.provider.iface.entity.base.OrderExtVo;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.drug.DeliveryVo;
import cn.yunyichina.scheduler.jobs.service.drug.DrugOrderAutoDealService;
import cn.yunyichina.scheduler.jobs.service.register.impl.RegisterUnlockServiceImpl;
import cn.yunyichina.scheduler.jobs.utils.RuleUtil;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qiniu.util.Json;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处方外购订单超时自动处理
 * Created by Administrator on 2017/8/1.
 */
@Service
public class DrugOrderAutoDealServiceImpl implements DrugOrderAutoDealService {
    private static final Logger logger = LoggerFactory.getLogger(DrugOrderAutoDealServiceImpl.class);

    @Override
    public void doExecute() {

        //开始查询超时订单
        DeliveryVo query = new DeliveryVo();
        String endTime = DateUtils.dateToString( new Date(System.currentTimeMillis() - 10*60*1000));
        query.setOrderStatus(DeliveryVo.ORDER_STATUS_PAYED);
        query.setEndPayTime(endTime);
        Response response = DrugService.callUnderlineFromInternal("yyt.drug.delivery.list",
                JsonUtils.toJsonString(query));
        logger.info("DrugOrderAutoDealServiceImpl doExecute, yyt.drug.delivery.lis response=" + JSON.toJSONString(response));
        if (!"0".equals(response.getResultCode())) {
            logger.error("doExecute, 查询订单失败");
            return ;
        }

        JSONObject jsonObject = JsonUtils.parseObject(response.getResult(),JSONObject.class);
        if(jsonObject != null && jsonObject.containsKey("list")){
            List<DeliveryVo> vos = JsonUtils.parseArray(JsonUtils.toJsonString(jsonObject.getJSONArray("list")), DeliveryVo.class);
            if(CollectionUtils.isEmpty(vos)){
                logger.info("DrugOrderAutoDealServiceImpl doExecute,没有订单需要自动处理");
                return;
            }
            for(DeliveryVo deliveryVo:vos){
//                String dealFlag = RuleUtil.getRule(deliveryVo.getCompanyCode(),RuleUtil.DRUG_AUTO_DEAL_TYPE,"2");//根据药店进行处理
                String dealFlag = RuleUtil.getRule(deliveryVo.getStoreCode(),RuleUtil.DRUG_AUTO_DEAL_TYPE,"2");//根据门店进行处理
                if(StringUtils.isBlank(dealFlag)){
                    logger.info("DrugOrderAutoDealServiceImpl doExecute 门店" + deliveryVo.getStoreCode() + "无配置超时订单处理规则，该订单" + deliveryVo.getOrderNo() + "暂不处理");
                    continue;
                }
                Map<String,String> map = new HashedMap();
                map.put("order_no",deliveryVo.getOrderNo());
                map.put("deal_flag",dealFlag);
                map.put("operators","system");
                Response autoResp = DrugService.callUnderlineFromInternal("yyt.drug.delivery.deal",
                        JsonUtils.toJsonString(map));
                logger.info("DrugOrderAutoDealServiceImpl doExecute,yyt.drug.delivery.deal,request={},response={}",
                        JsonUtils.toJsonString(map),JsonUtils.toJsonString(autoResp));
            }
        }else{
            logger.error("DrugOrderAutoDealServiceImpl doExecute 查询订单异常");
        }


    }

}
